<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>09-缓动动画</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        span {
            /* 动画一定要加定位 */
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <button id="btn">开启500</button>
    <div>一路向北</div>
    <script>
        /* 
            需求：点击按钮：div 移动 移动速度减慢 直接降为0

            步长取整：步长小于1px 就会出现各种问题
        */

        var btn = document.querySelector('button');
        var div = document.querySelector('div');

        btn.addEventListener('click',function(){
            console.log('div 开始移动');
            animate(div,500);
        });

        function animate(obj,target){
            clearInterval(obj.timer);
            obj.timer = setInterval(() => {
                let left = obj.offsetLeft;
                if(left > target){
                    clearInterval(obj.timer);
                    return;
                }
                // 步长 = (目标位置 - 当前位置) / !0
                let step = target - left;
                step = step / 10;
                step = Math.ceil(step);
                obj.style.left = left + step + 'px';
            },200);
        }
    </script>
</body>
</html>